package org.jxc.web.action;

import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.ArriveOrder;
import org.jxc.domain.InOrder;
import org.jxc.domain.InOrderProduct;
import org.jxc.domain.SalesBackOrder;
import org.jxc.domain.Warehouse;
import org.jxc.service.ArriveOrderService;
import org.jxc.service.InOrderProductService;
import org.jxc.service.InOrderService;
import org.jxc.service.SalesBackOrderService;
import org.jxc.service.StockService;
import org.jxc.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class InOrderAction extends GenericAction<InOrder> {

	private static final long serialVersionUID = 3780914758042156697L;

	@Autowired
	private ArriveOrderService arriveOrderService;
	@Autowired
	private InOrderService inOrderService;
	@Autowired
	private InOrderProductService inOrderProductService;
	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private StockService stockService;
	@Autowired
	private SalesBackOrderService salesBackOrderService;

	private String arriveOrderId;
	private String salesBackOrderId;
	private String inOrderId;
	private InOrder inOrder;
	private List<InOrderProduct> iopList;
	private List<Integer> amountsList;
	private List<String> iopIdList;
	private List<String> houseIdList;
	private List<Warehouse> warehouseList;
	private Integer auditState;

	public String save() throws Exception {
		if (arriveOrderId != null && !arriveOrderId.equals("")) {
			InOrder io = inOrderService
					.findInOrderByArriveOrderId(arriveOrderId);
			if (io == null) {
				inOrderId = inOrderService.saveForArrive(arriveOrderId);
			} else {
				inOrderId = io.getId();
			}
		} else {
			InOrder io = inOrderService
					.findInOrderBySalesBackOrderId(salesBackOrderId);
			if (io == null) {
				inOrderId = inOrderService.saveForBack(salesBackOrderId);
			} else {
				inOrderId = io.getId();
			}

		}
		return SUCCESS;
	}

	public String toUpdate() throws Exception {
		inOrder = inOrderService.findById(inOrderId);
		iopList = inOrderProductService.findByInOrderId(inOrderId);
		warehouseList = warehouseService.findAll();
		return SUCCESS;
	}

	public String update() throws Exception {
		inOrderService.update(inOrder);
		if (iopIdList != null) {
			for (int i = 0; i < iopIdList.size(); i++) {
				InOrderProduct temp = inOrderProductService.findById(iopIdList
						.get(i));
				temp.setWareHouse(warehouseService.findById(houseIdList.get(i)));
				temp.setInAmounts(amountsList.get(i));
				temp.setInPrice(temp.getInPrice());
				inOrderProductService.updateInOrderProduct(temp);
			}
		}
		return SUCCESS;
	}

	public String listPagination() throws Exception {
		page = inOrderService.listPagination(page);
		return SUCCESS;
	}

	public String toView() throws Exception {
		inOrder = inOrderService.findById(inOrderId);
		iopList = inOrderProductService.findByInOrderId(inOrderId);
		warehouseList = warehouseService.findAll();
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		for (String str : idList) {
			try {
				inOrderService.deleteInOrder(str);
			} catch (Exception e) {
				continue;
			}
		}
		return SUCCESS;
	}

	public String apply() throws Exception {
		inOrder = inOrderService.findById(inOrderId);
		inOrder.setAuditState(JXCString.APPLY);
		inOrderService.update(inOrder);
		return SUCCESS;
	}

	public String audit() throws Exception {
		inOrder = inOrderService.findById(inOrderId);
		inOrder.setAuditState(auditState);
		SalesBackOrder salesBackOrder = inOrder.getSalesBackOrder();
		if (salesBackOrder == null) {
			ArriveOrder ao = inOrder.getArriveOrder();
			if (auditState == 2) {
				ao.setStoreState(JXCString.ALLINSTORE);
			} else if (auditState == 3) {
				ao.setStoreState(JXCString.NOTINSTORE);
			}
			arriveOrderService.update(ao);
		} else {
			if (auditState == 2) {
				salesBackOrder.setStoreState(JXCString.ALLINSTORE);
			} else if (auditState == 3) {
				salesBackOrder.setStoreState(JXCString.NOTINSTORE);
			}
			salesBackOrderService.updateSalesBackOrder(salesBackOrder);
		}
		inOrderService.update(inOrder);
		List<InOrderProduct> iopList = inOrderProductService
				.findByInOrderId(inOrder.getId());
		for (InOrderProduct iop : iopList) {
			if (iop.getSalesBackOrderProduct() == null) {
				stockService.saveInStock(iop);
			} else {
				stockService.saveInStockForBack(iop);
			}
		}
		return SUCCESS;
	}

	public String getArriveOrderId() {
		return arriveOrderId;
	}

	public void setArriveOrderId(String arriveOrderId) {
		this.arriveOrderId = arriveOrderId;
	}

	public String getInOrderId() {
		return inOrderId;
	}

	public void setInOrderId(String inOrderId) {
		this.inOrderId = inOrderId;
	}

	public InOrder getInOrder() {
		return inOrder;
	}

	public void setInOrder(InOrder inOrder) {
		this.inOrder = inOrder;
	}

	public List<InOrderProduct> getIopList() {
		return iopList;
	}

	public void setIopList(List<InOrderProduct> iopList) {
		this.iopList = iopList;
	}

	public List<Integer> getAmountsList() {
		return amountsList;
	}

	public void setAmountsList(List<Integer> amountsList) {
		this.amountsList = amountsList;
	}

	public List<String> getIopIdList() {
		return iopIdList;
	}

	public void setIopIdList(List<String> iopIdList) {
		this.iopIdList = iopIdList;
	}

	public List<Warehouse> getWarehouseList() {
		return warehouseList;
	}

	public void setWarehouseList(List<Warehouse> warehouseList) {
		this.warehouseList = warehouseList;
	}

	public Integer getAuditState() {
		return auditState;
	}

	public void setAuditState(Integer auditState) {
		this.auditState = auditState;
	}

	public List<String> getHouseIdList() {
		return houseIdList;
	}

	public void setHouseIdList(List<String> houseIdList) {
		this.houseIdList = houseIdList;
	}

	public String getSalesBackOrderId() {
		return salesBackOrderId;
	}

	public void setSalesBackOrderId(String salesBackOrderId) {
		this.salesBackOrderId = salesBackOrderId;
	}
}
